<?php
class DbMysql
{
	protected  $prefix="";
	protected  static  $connection;
	protected  static  $client_count=0;
	protected $host;
	protected $user;
	protected $pword;
	protected $dbname;
	protected $charset="UTF-8";
	//单例模式
	public function __construct($param)
	{
		$this->host=$param["host"];
		$this->user=$param["user"];
		$this->pword=$param["pword"];
		$this->dbname=$param["dbname"];
		if(!empty($param["prefix"]))
		{
			$this->prefix=$param["prefix"];
		}
		if(isset($param["charset"]))
		{
			$this->charset=$param["charset"];
		}
		if(empty(self::$connection))
		{
			$this->getInstance($this->host,$this->user,$this->pword,$this->dbname,$this->charset);
		}
		self::$client_count++;
	}
	
	//如果所有的对象已经释放，则释放数据库连接
	public function __destruct()
	{
		self::$client_count--;
		if(self::$client_count==0)
		{
			mysql_close(self::$connection);
		}
	}
	private  function getInstance($host,$user,$pword,$dbname,$charset="UTF-8")
	{
		self::$connection=mysql_connect($host,$user,$pword,false,123456);
		mysql_select_db($dbname,self::$connection);
		mysql_query('set names '.$charset);
	}
	//参数SQL不能包含select如： * from member;
	public function select($sql,$sqlcount=false)
	{
		$result=array();
		$sql_front="select ";
		if($sqlcount)
		{
			$sql_front.=" SQL_CALC_FOUND_ROWS ";
		}
		$sql=$sql_front.$sql;
		$sql_result=mysql_query($sql,self::$connection);
		while ($row=mysql_fetch_assoc($sql_result))
		{
			$result[]=$row;
		}
		mysql_free_result($sql_result);
		return $result;
	}
	
	public function getLastQueryTotal()
	{
		$count=0;
		$sql="select FOUND_ROWS() as c";
		$sql=mysql_query($sql,self::$connection);
		$count=mysql_fetch_assoc($sql);
		mysql_free_result($sql);
		return $count;
	}
	
	public function insert($data,$table)
	{
		$sql_front="insert into $table(";
		$sql_value=" values(";
		foreach ($data as $field=>$value)
		{
			$sql_front.=$field.',';
			$sql_value.="'$value',";
			
		}
		$sql_front=substr($sql_front,0,strlen($sql_front)-1);
		$sql_front.=")";
		$sql_value=substr($sql_value,0,strlen($sql_value)-1);
		$sql_value.=")";
		mysql_query($sql_front.$sql_value,self::$connection);
		return mysql_insert_id();
	}
	
	public function delete($sql)
	{
		mysql_query($sql,self::$connection);
		return mysql_affected_rows(self::$connection);
	}
	
	public function update($data,$where,$table)
	{
		$sql="update $table set ";
		foreach ($data as $field=>$value)
		{
			$sql.=" $field='$value',";
		}
		$sql=substr($sql,0,strlen($sql)-1);
		$sql.=' where  '.$where;
		mysql_query($sql,self::$connection);
		return mysql_affected_rows(self::$connection);
	}
	
	public function startTransaction()
	{
		mysql_query("SET autocommit=0",self::$connection);
		mysql_query("START TRANSACTION",self::$connection);
	}
	
	public function getError()
	{
		return mysql_error(self::$connection);
	}
	
	public function rollBack()
	{
		mysql_query("ROLLBACK",self::$connection);
	}
	
	public function commit()
	{
		mysql_query("COMMIT",self::$connection);
	}
	
	public function execute($sql)
	{
		return mysql_query($sql);
	}
}